import { saveOrUpdate, getDetail, methodGetApi } from '@/api/publicApi'
// 混入
import { publicMixins } from '@/mixins/publicMixins/publicMixins'
// js
import resizeViewConfig from '@/utils/commonUtils/resizeViewConfig'// 全局更改大小
// dialogFormMixins
export const dialogFormMixins = {
  mixins: [publicMixins],
  inject: ['listQueryParams'],
  data() {
    return {
      // 是否显示弹出层
      dialogFormVisibleBool: false,
      // 遮罩层
      dialogFormLoading: false,
      // 弹窗title
      dialogFormTitle: '',
      // 弹窗状态，add:'新增'，edit:修改，view:'详情'
      dialogFormType: '',
      // 弹窗传值 额外入参
      dialogFormExtra: {}
    }
  },
  watch: {
    dialogFormVisibleBool(a, b) {
      console.log(a)
      console.log(b)
      if (a) {
        this.$nextTick(() => {
          resizeViewConfig()
        })
      }
    }
  },

  mounted() {
    window.addEventListener('resize', function() {
      resizeViewConfig()
    })
  },
  methods: {
    /**
     * @description: :新增/修改
     * @Author: baoxuhui
     * @param {*} url // 请求路径
     * @param {*} data // 入参
     * @param {*} type // 保存/修改.......
     * @param {*} resetBool // 回调后是否调取dialogResetFormMixins(type)    布尔类型 默认:false
     * @return {*}
     */
    dialogSaveOrUpdateMixins(url, data, type, resetBool) {
      console.log(type)
      return new Promise((resolve) => {
        this.dialogFormLoading = true
        saveOrUpdate(url, data).then(res => {
          this.msgAlt(type + '成功', 'success')
          if (this.sentencedEmpty(this.dialogForm.deleteAnnexIds) && this.dialogForm.deleteAnnexIds.length > 0) {
            const url = '/sys/annex/removeByIds'
            const dataIds = { ids: this.dialogForm.deleteAnnexIds.toString() }
            methodGetApi(url, dataIds).then(response => {
              if (!resetBool) {
                this.dialogResetFormMixins(type)
              }
            })
          } else {
            if (!resetBool) {
              this.dialogResetFormMixins(type)
            }
          }
          resolve(res)
        }).catch((erroe) => {
          resolve(erroe)
          this.dialogFormLoading = false
        })
      })
    },
    /**
     * @description: 表单详情
     * @Author: baoxuhui
     * @param {*} url // 请求路径
     * @param {*} data // 入参
     * @return {*}
     */
    dialogGetDetailMixins(url, data) {
      this.dialogFormLoading = true
      getDetail(url, data).then(res => {
        this.dialogFormDetail(res)// 返回每个表单的数据
      }).catch(() => {
        this.dialogFormLoading = false
      })
    },

    /**
     * @description: 弹框点击取消事件（需要再次构造）
     * @Author: baoxuhui
     * @param {*} type // 保存/修改.......
     * @return {*}
     */
    dialogResetFormMixins(type) {
      this.resetForm('dialogForm')
      this.dialogFormVisibleBool = false
      this.dialogFormLoading = false
      // 新增修改才调取
      // || type == '上传'
      if (type === '新增' || type === '修改' || type == '提交' || type == '上传') {
        console.log('-------',this.listQueryParams)
        this.listQueryParams.queryData.queryParams.pageNum = 1
        // this.listQueryParams.queryData.queryParams.pageSize = this.listQueryParams.queryData.queryParams.pageSize
        this.$nextTick(() => {
          if (this.listQueryParams.$refs['advancedQueryShrinkComponent']) {
            this.listQueryParams.$refs['advancedQueryShrinkComponent'].handleQueryFun()
          } else {
            this.listQueryParams.getList()
          }
        })
      }
    }

    // /**
    //  * @description: 弹框点击取消事件（2025/2/26重构）
    //  * @Author: baoxuhui
    //  * @param {*} closeBool//是否关闭关闭弹窗：boolean类型 默认:true
    //  * @param {*} queryBool//是否调取查询条件：boolean类型 默认:true
    //  * @return {*}
    //  */
    // dialogResetFormMixins(closeBool = true, queryBool = true) {
    //   this.resetForm('dialogForm')
    //   this.dialogFormLoading = false
    //   if (closeBool) {
    //     this.dialogFormVisibleBool = false
    //   }
    //   this.listQueryParams.queryData.queryParams.pageNum = 1
    //   if (queryBool) {
    //     this.listQueryParams.$refs['advancedQueryShrinkComponent'].handleQueryFun()
    //   } else {
    //     this.listQueryParams.getList()
    //   }
    // }
  }
}
